<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>07_猜数字练习</title>
</head>
<body>
<h1>已生成一个1~100的随机数</h1>
<input placeholder="请输入你猜的数字!" id="n1">
<button onclick="f1()">点我验证</button>
<h1>小提示: <span></span></h1>
<script>
    let num = parseInt((Math.random()*100)+1);
    console.log('打个小抄:'+num)
    let n = 0;
    function f1(){
        let i1 = document.getElementById('n1');
        let spane = document.querySelector("span");
        let i2 = parseInt(i1.value);
        if (!/^\d+$/.test(i2)){
            alert('请输入数字！')
            return;
        }
        // if (isNaN(i2)){
        //     alert('请输入数字!');
        //     return;
        // }
        console.log(i2);
            if (i2>num){
                spane.innerHTML='猜大了';
                n++;
            }else if(i2<num){
                spane.innerHTML='猜小了';
                n++;
            }else {
                spane.innerHTML='恭喜你用了'+n+'次猜对了'
            }
    }
</script>
</body>
</html>